package com.sunjq.multithread;

import java.util.ArrayList;
import java.util.List;

public class Fibonaci implements Runnable{
	
	private int result;
	
	private int number;
	
	private static int threadCount = 0;
	
	private final int currentThread = threadCount ++;
	
	public Fibonaci (int number) {
		this.number = number;
	}
	
	private List<Integer> resultList = new ArrayList<Integer>();

	

	public void setResultList(List<Integer> resultList) {
		this.resultList = resultList;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		int arg = Integer.parseInt(args[0]);

		Fibonaci fibonaci1 = new Fibonaci(arg);
		Fibonaci fibonaci2 = new Fibonaci(arg);
		Thread t1 = new Thread(fibonaci1);
		Thread t2 = new Thread(fibonaci2);
		t1.start();
		t2.start();
//		fibonaci.generateFibList(3);
//		List<Integer> list = fibonaci.getResultList();
//		for (Integer i : list) {
//			System.out.print(i + ", ");
//		}

	}

	public void run() {
//		int i = this.genFibonacciValue(this.number);
//		System.out.println("CurrentThread: " + currentThread + ", v: " + i + ", ");
//		if (i > 5) {
//			Thread.yield();
//		}
		this.generateFibonacciList(number);
		if (this.resultList.size() > 10) {
			Thread.yield();
		}
	}
	
	private int genFibonacciValue(int number) {
		if (number == 0) {
//			System.out.println("number = 0");
			result = 1;
//			System.out.println("result = 1");
			
			
		}
		else if (number == 1) {
//			System.out.println("number = 1");
			result = 1;
//			System.out.println("result = 1");
			
			
		} else {
//			System.out.println("number = " + number);
			result = genFibonacciValue(number - 1) + genFibonacciValue(number - 2);
//			System.out.println("result = " + result);
			
		}
		return result;
		
	}
	
	public void generateFibonacciList(int number) {
		for (int i = 0; i < number; i ++) {
			int result = this.genFibonacciValue(i);
			System.out.println("CurrentThread: " + currentThread + ", v: " + result + ", ");
			resultList.add(result);
		}
		
	
	}

}
